//
//  HomeTableViewController.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/6/13.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //1.判断用户是否登录
        if !isLogin{
            visitorView?.setupVisitorInfo(imageName: nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //2.添加导航条按钮
        setNavBar()
        
        //3.注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(rawValue: WZYPresentationManagerDidPresented), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(rawValue: WZYPresentationManagerDidDismissed), object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    //MARK: -内部控制器方法
    private func setNavBar(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
       
        navigationItem.titleView = titleBtn
    }
    
    @objc private func titleBtnClick(btn:TitleButton){
        //1.修改按钮状态
        
        //2.显示菜单
        let sb = UIStoryboard(name: "Popover", bundle: nil)
        guard let menuView = sb.instantiateInitialViewController() else {
            return
        }
        
        //自定义转场动画
        //设置转场代理
        menuView.transitioningDelegate = animatorManager
        //设置转场动画样式
        menuView.modalPresentationStyle = UIModalPresentationStyle.custom
        
        present(menuView, animated: true, completion: nil)
        
    }
    
    @objc private func leftBtnClick(){
    
    }
    
    
    @objc private func rightBtnClick(){
        //创建二维码控制器
        let sb = UIStoryboard(name: "QRCode", bundle: nil)
        let vc = sb.instantiateInitialViewController()!
        
        //弹出二维码控制器
       present(vc, animated: true, completion: nil)
    }
    
    
    @objc private func titleChange(){
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
    //MARK: -懒加载
    private lazy var animatorManager:WZYPresentationManager = {
        let manager =  WZYPresentationManager()
        manager.presentFrame = CGRect(x: 100, y: 45, width: 200, height: 400)
        return manager
    }()
    
    private lazy var titleBtn: TitleButton = {
//        () -> TitleButton
//        in
        let btn = TitleButton()
        btn.setTitle("这是个测试账号", for: .normal)
        btn .addTarget(self, action: #selector(titleBtnClick(btn: )), for: .touchUpInside)
        return btn
    }()
    
   
    
}



